package net.example.activity.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import net.example.manager.ClientGlobalCache;
import net.example.model.GGUser;
import net.example.model.UserBasicModel;
import net.example.ggtalk.R;
import net.example.utils.HeadImgHelper;

import java.util.List;

public class HeaderAdapter extends BaseAdapter {
    private List<UserBasicModel> modelList;
    private Context context;

    public HeaderAdapter(List<UserBasicModel> userList, Context context)
    {
        this.context=context;
        this.modelList =userList;
    }
    @Override
    public int getCount() {
        return this.modelList.size();
    }

    @Override
    public Object getItem(int position) {
        return this.modelList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        try{
            HeaderAdapter.HeaderViewHolder holder;
            if (view == null) {
                view = LayoutInflater.from(context).inflate(
                        R.layout.group_header_item, parent, false);
                holder = new HeaderAdapter.HeaderViewHolder(view);
                view.setTag(holder);
            } else {
                holder = (HeaderAdapter.HeaderViewHolder) view.getTag();
            }
            UserBasicModel model = this.modelList.get(position);

            if(model.getHeadImageUrl()!=0)
            {
                holder.headImg.setImageResource(model.getHeadImageUrl());
            }
            else {
                GGUser GGUser = ClientGlobalCache.getInstance().getUser(model.getUserID());
                HeadImgHelper.setUserHeadImg(holder.headImg, GGUser);
            }
            holder.userName.setText(model.getUserName());
        }catch (Exception ex){ex.printStackTrace();}
        return view;
    }

    private class HeaderViewHolder
    {
        public ImageView headImg;
        public TextView userName;

        public HeaderViewHolder(View view)
        {
            this.headImg = (ImageView) view.findViewById(R.id.group_header_img);
            this.userName=(TextView) view.findViewById(R.id.group_username);
        }
    }
}

